<template>
    <div class="container">
      <el-row>
        <el-col :span="8" class="left-column">
          <div class="avatar-wrapper">
            <img :src="userInfo.avatar" alt="头像">
          </div>
          <div class="user-info">
            <h3>{{ userInfo.uname }}</h3>
            <p>{{ userInfo.tel }}</p>
          </div>
          <div class="nav-menu">
            <el-menu default-active="1" class="el-menu-vertical-demo" @select="handleSelect">
              <el-menu-item index="1">
                <i class="el-icon-user"></i>
                <span slot="title">个人资料</span>
              </el-menu-item>
              <el-menu-item index="2">
                <i class="el-icon-message"></i>
                <span slot="title">消息</span>
              </el-menu-item>
              <el-menu-item index="3">
                <i class="el-icon-setting"></i>
                <span slot="title">设置</span>
              </el-menu-item>
            </el-menu>
          </div>
        </el-col>
        <el-col :span="16" class="right-column">
          <h2 v-if="activeTab === '1'">
            个人资料
            <el-button type="primary" size="small" @click="handleEdit">编辑</el-button>
          </h2>
          <h2 v-else-if="activeTab === '2'">消息</h2>
          <h2 v-else-if="activeTab === '3'">设置</h2>
          <div v-if="activeTab === '1'">
            <el-form :model="userInfo" :rules="userRules" ref="userInfoForm" label-width="100px" class="user-form">
              <el-form-item label="用户名" prop="name">
                <el-input v-model="userInfo.uname"></el-input>
              </el-form-item>
              <el-form-item label="手机号" prop="phone">
                <el-input v-model="userInfo.tel"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="pwd">
                <el-input v-model="userInfo.pwd"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="userInfo.email"></el-input>
              </el-form-item>
              <el-form-item label="头像" prop="avatar">
                <el-upload
                  class="avatar-uploader"
                  action="/your-avatar-upload-api-url"
                  :show-file-list="false"
                  :before-upload="beforeAvatarUpload"
                  :on-success="handleAvatarSuccess"
                  :on-error="handleAvatarError">
                  <img v-if="userInfo.avatar" :src="userInfo.avatar" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleSubmit">提交</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div v-else-if="activeTab === '2'">
            <!-- 在此处添加消息内容 -->
          </div>
          <div v-else-if="activeTab === '3'">
            <!-- 在此处添加设置选项 -->
          </div>
        </el-col>
      </el-row>
    </div>
  </template>
  
  
  
  <script lang="ts">
import axios from 'axios';

export default {
  data() {
    return {
      userInfo: {
        avatar: 'https://tupian.qqw21.com/article/UploadPic/2020-4/20204823154758937.jpg',
        uname: '123',
        tel: '13993236249',
        pwd:'sw1234',
        email: 'your-email@example.com'
      },
      activeTab: '1',
      userRules: {
        uname: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] }
        ]
      }
    };
  },
  methods: {
    handleSelect(index) {
      this.activeTab = index;
    },
    handleEdit() {
      // 编辑按钮点击事件
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
        return false;
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
        return false;
      }
      return true;
    },
    handleAvatarSuccess(response) {
      if (response.code === 200) {
        this.userInfo.avatar = response.data.avatarUrl;
        this.$message.success('头像上传成功!');
      } else {
        this.$message.error('头像上传失败!');
      }
    },
    handleAvatarError() {
      this.$message.error('头像上传失败!');
    },
    handleSubmit() {
      this.$refs.userInfoForm.validate((valid) => {
        if (valid) {
          // 表单校验通过
          const formData = new FormData();
          formData.append('uname', this.userInfo.uname);
          formData.append('tel', this.userInfo.tel);
          formData.append('email', this.userInfo.email);
          formData.append('avatar', this.userInfo.avatar);
          formData.append('pwd', this.userInfo.pwd);
          axios.post('/egg/getInfo', formData)
            .then(response => {
                console.log(response.data);
              if (response.data.data === 200) {
                this.$message.success('用户信息更新成功!');
               

              }

              
              else {
                response.data===400
                this.$message.error('用户信息更新失败!');
              }
            })
        }
      });
    }
  }
};
  </script>
  
  <style scoped>
 .container {
  padding: 2rem;
  margin-top:100px;
}

.left-column {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.avatar-wrapper {
  width: 8rem;
  height: 8rem;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 2rem;
}

.avatar-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-info {
  text-align: center;
}

.nav-menu {
  margin-top: 2rem;
}

.right-column {
  padding-left: 2rem;
  border-left: 1px solid #ccc;
}

h2 {
  font-size: 24px;
  margin-bottom: 2rem;
}

.user-form {
  max-width: 500px;
  margin: 0 auto;
}

.avatar-uploader {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 150px;
  height: 150px;
  border: 1px dashed #ccc;
  border-radius: 50%;
  cursor: pointer;
  overflow: hidden;
  position: relative;
}

.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #999;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader i {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}


</style>